package com.rr.mymoney.jsf;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.faces.bean.ManagedProperty;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rr.mymoney.bo.ReportsBo;
import com.rr.mymoney.jsf.util.FilterUtils;
import com.rr.mymoney.model.util.ReportItem;
import com.rr.mymoney.utils.DateUtils;
import com.rr.mymoney.utils.Filter;

// Spring instantiates the object, so there is no use for JSF annotations
@Controller
@Scope("view")
public class HistoryController extends AbstractController {

    private static final long serialVersionUID = 7148350955893879334L;

    private static transient Logger log = Logger.getLogger(HistoryController.class);

    private transient ReportsBo reportsBo;

    private List<ReportItem> list;

    private ReportItem selectedItem;

    @ManagedProperty(value = "#{filter}")
    private transient Filter filter = FilterUtils.getFilterFromSession("historyFilter");

    @Autowired
    public void setReportsBo(ReportsBo reportsBo) {
	this.reportsBo = reportsBo;
    }

    public void filter() {
	log.debug(filter);
	FilterUtils.saveFilterToSession(filter, "historyFilter");

	list = reportsBo.getStatisticsData(getLoggedUser().getId(), 
		filter.getYearMonth() != null ? DateUtils.getMonth(filter.getYearMonth()) : DateUtils.getCurrentMonth(),
		filter.getYearMonth() != null ? DateUtils.getYear(filter.getYearMonth()) : DateUtils.getCurrentYear(),
		filter.isGroupByYear(), 
		filter.isShowAllMonths(),
		filter.isShowAllYears(),
		filter.isGroupByAccount(),
		filter.isGroupByCategory(), 
		filter.getAccount() != null && filter.getAccount().getId() != null && filter.getAccount().getId() > 0 ? filter.getAccount().getId() : null, 
		filter.getCategory() != null && filter.getCategory().getId() != null && filter.getCategory().getId() > 0 ? filter.getCategory().getId() : null,
		filter.getCurrency() != null && !StringUtils.isEmpty(filter.getCurrency().getId()) ? filter.getCurrency().getId() : null,
		filter.isShowIgnored());
    }

    public List<ReportItem> getData() {
	if (list == null) {
	    filter();
	}
	return list;
    }

    /**
     * @return the selectedItem
     */
    public ReportItem getSelectedItem() {
	return selectedItem;
    }

    /**
     * @param selectedItem
     *            the selectedItem to set
     */
    public void setSelectedItem(ReportItem selectedItem) {
	this.selectedItem = selectedItem;
    }

    public void viewTransactions() {

    }

    /**
     * @return the filter
     */
    public Filter getFilter() {
	return filter;
    }

    /**
     * @param filter
     *            the filter to set
     */
    public void setFilter(Filter filter) {
	this.filter = filter;
    }

    public void updateToPreviousYear() {
	filter.setYear(filter.getYear() - 1);
    }

    public void updateToNextYear() {
	filter.setYear(filter.getYear() + 1);
    }

    public void updateToPreviousMonth() {
	int prevYearMonth = DateUtils.getPreviousMonth(filter.getYear() * 100 + filter.getMonth());
	filter.setYear(DateUtils.getYear(prevYearMonth));
	filter.setMonth(DateUtils.getMonth(prevYearMonth));
    }

    public void updateToNextMonth() {
	int prevYearMonth = DateUtils.getNextMonth(filter.getYear() * 100 + filter.getMonth());
	filter.setYear(DateUtils.getYear(prevYearMonth));
	filter.setMonth(DateUtils.getMonth(prevYearMonth));
    }

    public String getMonthDisplay() {
	DateFormat df = new SimpleDateFormat("MMMM");
	return df.format(DateUtils.getStartDate(filter.getYear(), filter.getMonth()));
    }
    
    public void setMonthDisplay(String monthDisplay) {
    }
}
